import { defineStore } from "pinia";

export const AccountStore = defineStore('useAccountStore', () => {
    let token = localStorage.getItem('TOKEN') || ''
    const state = {
        token:'' as string, // 使用从localStorage获取的token作为初始值
        menus: [] as any[],
        userinfo: {} as object
    }
    if (token) state.token = token;
    // 定义actions
    function setMenus(data:  any) {
        state.menus = data;
    }

    function setUserInfo(data: any) {
        state.userinfo = data;
    }

    function setToken(token: string) {
        state.token = token;
        // 可以在这里添加将token保存到localStorage的逻辑
        localStorage.setItem('TOKEN', token);
    }
    function logout() {
        setToken('');
        setUserInfo({});
        setMenus([]);
        state.token = '';
        localStorage.removeItem('TOKEN');
    }
    // 返回state和actions
    return {
        state,
        setMenus,
        setUserInfo,
        setToken,
        logout,
    };
})